package com.example.dell.fastshopping.DuoBao.model;

import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.dell.fastshopping.DuoBao.entity.PriductInfo;
import com.example.dell.fastshopping.DuoBao.entity.RoundResult;
import com.example.dell.fastshopping.R;
import com.example.dell.fastshopping.mall.entity.ServerTime;
import com.example.dell.fastshopping.network.ApiService;
import com.example.dell.fastshopping.network.NetCallback;
import com.example.dell.fastshopping.network.RetrofitClient;
import com.example.dell.fastshopping.network.http.BaseResponse;
import com.example.dell.fastshopping.user.entity.IntegralBaseInformation;
import com.example.dell.fastshopping.utils.KLog;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoaderInterface;

import java.net.HttpCookie;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.http.Url;

/**
 * Created by ${YSH} on 2019/4/13.
 * 描述        ${TODO}
 * <p>
 * 更新者       $author$
 * 更新时间     $Data$
 * 描述         ${TODO}
 */
public class DuoBaoActViewModel extends ViewModel {
    private ApiService service = RetrofitClient.getInstance().create(ApiService.class);
    private MutableLiveData<PriductInfo> mallLists = new MutableLiveData<>();
    private MutableLiveData<ServerTime> serverTime = new MutableLiveData<>();
    private MutableLiveData<BaseResponse> jinPaiAction = new MutableLiveData<>();
    private MutableLiveData<RoundResult> roundResult = new MutableLiveData<>();
    private MutableLiveData<List<ServerTime.DuobaoHistory>> historyList = new MutableLiveData<>();
    private MutableLiveData<String> introduce = new MutableLiveData<>();
    private MutableLiveData<IntegralBaseInformation> userintegral = new MutableLiveData<>();

    public void setBanner(Banner banner, String pic) {
        String[] list = pic.split(",");
        List<String> asList = Arrays.asList(list);
        //设置图片网址或地址的集合
        banner.setImages(asList);
        //设置轮播间隔时间
        banner.setDelayTime(3000);
        //设置是否为自动轮播，默认是“是”。
        banner.isAutoPlay(true);
        banner.setBannerStyle(BannerConfig.CENTER);
        //设置轮播的动画效果，内含多种特效，可点入方法内查找后内逐一体验
        banner.setBannerAnimation(Transformer.Default);
        banner.setImageLoader(new ImageLoaderInterface() {
            @Override
            public void displayImage(Context context, Object path, View imageView) {
                Glide.with(context).load(path).centerCrop().error(R.mipmap.banner_defaule).into((ImageView) imageView);
            }

            @Override
            public View createImageView(Context context) {
                return null;
            }
        });
        //设置指示器的位置，小点点，左中右。
        banner.setIndicatorGravity(BannerConfig.RIGHT)
                //以上内容都可写成链式布局，这是轮播图的监听。比较重要。方法在下面。
                .setOnBannerListener(new OnBannerListener() {
                    @Override
                    public void OnBannerClick(int position) {
                    }
                }).start();
    }

    public String[] getTimeArray(long millis) {

        long time = millis / 1000;
        if (time <= 0) {
            return new String[]{"00", "00", "00","0"};
        }

        String[] str;
        int hour = 0;
        int minute = 0;
        int second = 0;
        minute = (int) time / 60;
        if (minute < 60) {
            second = (int) (time % 60);
            str = new String[]{"00", unitFormat(minute), unitFormat(second),second+""};
        } else {
            hour = minute / 60;
            if (hour > 99)
                return new String[]{"99", "59", "59"};
            minute = minute % 60;
            second = (int) (time - hour * 3600 - minute * 60);
            str = new String[]{unitFormat(hour), unitFormat(minute), unitFormat(second),second+""};
        }
        return str;
    }

    public String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

    public LiveData<PriductInfo> getProductInfo(int id) {
        RetrofitClient.execute(service.getProductinfo(id), new NetCallback<BaseResponse<PriductInfo>>() {
            @Override
            public void netCallback(BaseResponse<PriductInfo> response) {
                mallLists.setValue(response.getData());
            }
        });
        return mallLists;
    }

    public LiveData<ServerTime> checkServerTime(int auctionClassId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("auctionClassId", auctionClassId);
        hashMap.put("round", 1);//后台暂未用到。写死
        String jsonString = new Gson().toJson(hashMap);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonString);
        RetrofitClient.execute(service.checkServerTime(requestBody), new NetCallback<BaseResponse<ServerTime>>() {
            @Override
            public void netCallback(BaseResponse<ServerTime> response) {
                serverTime.setValue(response.getData());
            }
        });
        return serverTime;
    }

    //userId-用户id | auctionRoundsId-轮次id | userName-用户名 | auctionClassId-竞拍场ID | price-本次出价的价格 | round-本轮次
    public LiveData<BaseResponse> doJinPaiAction(int auctionRoundsId, int auctionClassId, int price, int currentRound) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("auctionRoundsId", auctionRoundsId);
        hashMap.put("auctionClassId", auctionClassId);
        hashMap.put("price", price);
        hashMap.put("round", currentRound);
        String json = new Gson().toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        RetrofitClient.executeBackAll(service.doJinPaiAction(body), new NetCallback<BaseResponse>() {
            @Override
            public void netCallback(BaseResponse response) {
                jinPaiAction.setValue(response);
            }
        });
        return jinPaiAction;
    }
//@Query("id") int id, @Query("currentRounds") int currentRounds, @Query("auctionClassId") int auctionClassId
    public LiveData<RoundResult> getCurrentRoundResult(int roundId, int currentRounds, int auctionClassId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("id",roundId);
        hashMap.put("currentRounds",currentRounds);
        hashMap.put("auctionClassId",auctionClassId);
        String s = new Gson().toJson(hashMap);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), s);
        RetrofitClient.execute(service.getCurrentRoundResult(body), new NetCallback<BaseResponse<RoundResult>>() {
            @Override
            public void netCallback(BaseResponse<RoundResult> response) {
                roundResult.setValue(response.getData());
            }
        });
        return roundResult;
    }


    public MutableLiveData<List<ServerTime.DuobaoHistory>> getHistoryList() {
        return historyList;
    }

    public MutableLiveData<String>  getGoodInfo() {
        return introduce;
    }


    public MutableLiveData<IntegralBaseInformation> getUserIntegral() {
        RetrofitClient.execute(service.getUserIntegral(), new NetCallback<BaseResponse<IntegralBaseInformation>>() {
            @Override
            public void netCallback(BaseResponse<IntegralBaseInformation> response) {
                userintegral.setValue(response.getData());
            }
        });
        return userintegral;
    }
}
